package org.dingfugui.triple.connection;

import org.dingfugui.triple.common.Logger;
import org.dingfugui.triple.common.LoggerFactory;

import java.nio.ByteBuffer;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

/**
 * @author fugui.ding at 2015/6/24
 */
public class Reader {
    private final Logger logger = LoggerFactory.getLogger(getClass());
    private final Future<Integer> future;
    private final ByteBuffer bb;

    public Reader(Future<Integer> future, ByteBuffer bb) {

        this.future = future;
        this.bb = bb;
    }

    public byte[] getData() {
        try {
            future.get();

            bb.flip();
            byte[] result = new byte[bb.limit()];

            bb.get(result, 0, bb.limit());
            logger.trace("读取返回报文长度:{}", result.length);
            return result;
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        return null;
    }

}
